package dgcptic.interventions.model;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import dgcptic.interventions.utils.EMF;

public class DemandeFactory {

	public DemandeFactory() {
	}
	
	
	public List<Demande> getListDemandes() {
		EntityManagerFactory emf = EMF.get();
		EntityManager em = emf.createEntityManager();
		Query query  = em.createQuery("select from "+Demande.class.getName());
		@SuppressWarnings("unchecked")
		List<Demande>  list =  query.getResultList();
		return list;
	}
	
	
	/*public List<Demande> getListDemandesDebudTemp() {
		 List<Demande> l = new ArrayList<Demande>();
		 for(int i =0; i<12; i++){
			 Demande d = new Demande("titre "+i,"Detail demande "+i);
			 l.add(d);
		 }
		 return l;
	}*/
	
	public void persists(Demande demand){
		EntityManagerFactory emf = EMF.get();
		EntityManager em = emf.createEntityManager();
		em.persist(demand);
		em.close();
	}
	
	public Demande getDemande(String idDemande){
		EntityManagerFactory emf = EMF.get();
		EntityManager em = emf.createEntityManager();
		Query query = em.createQuery("select from " + Demande.class.getName()+" where id = :1" );
		query.setParameter(1, idDemande);
		List<Demande> list = query.getResultList();
		
		if(list.size()==0) {
			return null;
		}
		else{
			return list.get(0);
		}
	}

}
